package org.friping.mytool.views.picker;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.widget.DatePicker;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.friping.mytool.R;
import org.friping.mytool.helper.Constants;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@EActivity(R.layout.p_date)
public class PickerDateActivity extends PickerActivity {

    @ViewById
    DatePicker datePicker;

    @AfterViews
    public void afterView(){
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        // render title
        if(title != null){
            actionBar.setTitle(title);
        }else{
            if(label != null){
                actionBar.setTitle("更改"+label);
            }
        }

        // render value
        if(value == null){
            value = new Date();
        }

        Calendar cal= Calendar.getInstance();
        cal.setTime((Date) value);

        datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null);
    }

    @Click(R.id.btnSave)
    public void save(){
        String value = datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" + datePicker.getDayOfMonth();
        Intent intent=new Intent();
        intent.putExtra("name", name);
        try{
            Date date = new SimpleDateFormat("y-M-d").parse(value);
            intent.putExtra("value", date);
        }catch (Exception e){
            e.printStackTrace();
        }
        setResult(Constants.PICKER_DATE_RESULT_CODE,intent);
        finish();
    }

}
